Ext.define('WebOs.ex.DefaultPaggingBar', {
	extend : 'Ext.toolbar.Paging',
	alias : 'widget.cnpbar',

	displayInfo : true,
	beforePageText : '第',
	afterPageText : '页-共{0}页',
	displayMsg : '显示 {0}-{1}条 ,共{2}条',
	emptyMsg : "暂无数据",
	dock : 'bottom',

	getPagingItems : function() {
		var me = this;

		return [{
					xtype : 'button',
					text : '导出Excel',
					icon : Constants.SER_RES_PATH + 'icon/share/excel_gen_small.png',
					handler : me.exportExcel
				}, '->', {
					itemId : 'first',
					tooltip : me.firstText,
					overflowText : me.firstText,
					iconCls : Ext.baseCSSPrefix + 'tbar-page-first',
					disabled : true,
					handler : me.moveFirst,
					scope : me
				}, {
					itemId : 'prev',
					tooltip : me.prevText,
					overflowText : me.prevText,
					iconCls : Ext.baseCSSPrefix + 'tbar-page-prev',
					disabled : true,
					handler : me.movePrevious,
					scope : me
				}, me.beforePageText, {
					xtype : 'numberfield',
					itemId : 'inputItem',
					name : 'inputItem',
					cls : Ext.baseCSSPrefix + 'tbar-page-number',
					allowDecimals : false,
					minValue : 1,
					hideTrigger : true,
					enableKeyEvents : true,
					keyNavEnabled : false,
					selectOnFocus : true,
					submitValue : false,
					// mark it as not a field so the form will not catch it when
					// getting
					// fields
					isFormField : false,
					width : me.inputItemWidth,
					margins : '-1 2 3 2',
					listeners : {
						scope : me,
						keydown : me.onPagingKeyDown,
						blur : me.onPagingBlur
					}
				}, {
					xtype : 'tbtext',
					itemId : 'afterTextItem',
					text : Ext.String.format(me.afterPageText, 1)
				}, {
					itemId : 'next',
					tooltip : me.nextText,
					overflowText : me.nextText,
					iconCls : Ext.baseCSSPrefix + 'tbar-page-next',
					disabled : true,
					handler : me.moveNext,
					scope : me
				}, {
					itemId : 'last',
					tooltip : me.lastText,
					overflowText : me.lastText,
					iconCls : Ext.baseCSSPrefix + 'tbar-page-last',
					disabled : true,
					handler : me.moveLast,
					scope : me
				}, {
					itemId : 'refresh',
					tooltip : me.refreshText,
					overflowText : me.refreshText,
					iconCls : Ext.baseCSSPrefix + 'tbar-loading',
					handler : me.doRefresh,
					scope : me
				}];
	},

	initComponent : function() {
		var me = this;
		Ext.applyIf(me, {
			items : ['->', {
						xtype : 'combo',
						width : 150,
						fieldLabel : '每页显示记录数',
						queryMode : 'local',
						displayField : 'num',
						valueField : 'num',
						action : 'pageSizeValueField',
						labelAlign : 'right',
						store : Ext.create('Ext.data.Store', {
									fields : ['num'],
									data : [{
												num : Constants.DEFAULT_PAGE_SIZE
											}, {
												num : 50
											}, {
												num : 100
											}, {
												num : 300
											}, {
												num : 500
											}, {
												num : 1000
											}]
								})
					}, {
						xtype : 'button',
						text : '设置',
						listeners : {
							click : me.onPageSizeBtnClick
						}
					}],
			listeners : {
				beforerender : me.initPagingBarData
			}
		})
		me.callParent(arguments);
		me.initExcelIFrame();
		me.down('combo').select(Constants.DEFAULT_PAGE_SIZE);
	},

	initExcelIFrame : function() {
		var iframe = document.getElementById('excelGen');
		if (!iframe) {
			Ext
					.getBody()
					.insertHtml('afterBegin',
							'<iframe id="excelGen" style="display:none" src="genExcel.html"></iframe>');
		}
	},

	initPagingBarData : function(cmp) {
		var me = this;
		me.bindStore(me.getStore());
	},

	onPageSizeBtnClick : function(btn) {
		var pageSize = btn.up('cnpbar')
				.down('textfield[action=pageSizeValueField]').getValue();
		var store = btn.up('cnpbar').getStore();
		store.pageSize = pageSize;
		store.loadPage(1);
	},
	/**
	 * 点击导出excel按钮触发函数
	 * 
	 * @param {}
	 *            btn
	 */
	exportExcel : function(btn) {
		var grid = btn.up('gridpanel');
		if (grid) {
			var store = grid.getStore();
			if (store.count() != 0) {
				var data = this.up('cnpbar').getGridData(grid);
				if (data) {
					this.up('cnpbar').requestExcel(data);
				}
			} else {
				Ext.Msg.alert('错误', "表格没有数据");
			}
		} else {
			Ext.Msg.alert('错误', "没有找到表格，无法导出");
		}
	},

	requestExcel : function(data) {
		var idoc = document.getElementById("excelGen").contentDocument;
		idoc.getElementById('input_title').value = data['title'];
		idoc.getElementById('input_header').value = Ext.encode(data['header']);
		idoc.getElementById('input_records').value = Ext
				.encode(data['records']);
		idoc.getElementById('excel_data_form').submit();
	},

	/**
	 * 获取表格的格式化数据
	 * 
	 * @param {}
	 *            grid
	 * @return {}
	 */
	getGridData : function(grid) {
		var data = {}
		var headers = [], index = [];
		var columns = grid.columns;
		data.title = grid.up('vf_tradition_contentPanel')
				? grid.up('vf_tradition_contentPanel').title
				: grid.up('window').title;
		for (var p in columns) {
			var col = columns[p];
			// 屏蔽不正确的column
			if (col.text != '' && col.dataIndex != '') {
				headers.push(col.text);
				index.push(col.dataIndex);
			}
		}
		data['header'] = headers;
		data.records = [];
		var store = grid.getStore();
		store.each(function(record) {
					var row = [];
					for (var i = 0; i < index.length; i++) {
						var field = record.get(index[i]);
						// 对日期进行格式化处理
						if (Ext.typeOf(field) == 'date') {
							row.push(Ext.Date.format(field, 'Y-m-d H:i:s'));
						} else {
							row.push(field);
						}
					}
					data.records.push(row);
				});
		return data;
	}
});